﻿<%@ WebHandler Language="C#"  Class="GovernmentOfficeProject.handler.InformSmsHandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.SysTools;
using GovernmentOfficeProject.LevelBusinessLogic;
using System.Web.SessionState;
using System.Collections;

namespace GovernmentOfficeProject.handler
{
    /// <summary>
    /// SystemContactsDatafeed 的摘要说明
    /// </summary>
    public class InformSmsHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0)
            {
                context.Response.Redirect("~/Login.htm?FromUrl=" + HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
            }
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["action"];
            string ret = "";
            switch (action)
            {
                case "save": ret = saveSmsReady(); break;
            }
            context.Response.Write(ret);
        }

        public string saveSmsReady()
        {
            HttpContext context = System.Web.HttpContext.Current;
            string content = context.Request.Params["smsContent"];
            string data = context.Request.Params["data"];
            List<SelectedContacts> contacts = JsonHelper.FromJson<List<SelectedContacts>>(context.Request.Params["data"]);
            List<Entity_COM_Sms_Ready> sysReadyList = new List<Entity_COM_Sms_Ready>();
            foreach (SelectedContacts obj in contacts)
            {
                Entity_COM_Sms_Ready smsReadyEntity = new Entity_COM_Sms_Ready();
                smsReadyEntity.RdyFromUserID = SystemContext.CurrentUserID;  //
                smsReadyEntity.RdyAccessType = "informSms";   //
                smsReadyEntity.RdyAccessValue = 2003;  //
                smsReadyEntity.RdyMtnumber = obj.MobileNumber;
                smsReadyEntity.RdyTextContent = "政府办信息系统通知:"+content;
                smsReadyEntity.RdySendFlag = 0;   //未发送
                smsReadyEntity.RdySendType = 0;  //立即发送
                sysReadyList.Add(smsReadyEntity);
            }
            bool isSuccess = false;
            Hashtable ht = new Hashtable();
            isSuccess = smsListReadySend(sysReadyList);
            if (isSuccess)
            {

                ht.Add("IsSuccess", true);
                ht.Add("Msg", "保存成功！");
            }
            else
            {
                ht.Add("IsSuccess", false);
                ht.Add("Msg", "保存失败!");
            }
            return JsonHelper.ToJson(ht);
        }

        //短信列表发送方法:
        private bool smsListReadySend(List<Entity_COM_Sms_Ready> srList)
        {
            try
            {
                bool flag = true;
                BLL_SmsOperation bllSms = new BLL_SmsOperation();
                foreach (Entity_COM_Sms_Ready sr in srList)
                {
                    if (!bllSms.addSmsReday(sr))
                    {
                        flag = false;
                    }
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 内部类 已选择的联系人 
        /// </summary>
        class SelectedContacts
        {
            public decimal Id{get;set;}
            public string Name { get; set; }
            public string MobileNumber { get;set;}
            public string type { get; set; }
            public string Company { get; set; }
        }
    }
}